﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.IO;

namespace YouDown.Models
{
    public enum VideoProcessState
    {
        Unprocessed,
        Processing,
        Done,
        Error
    }

    public class Video : INotifyPropertyChanged
    {
        private string title;
        private string error;
        private QualityLevel quality;
        private Uri address;
        private string id;
        private int l;
        private string t;
        private VideoProcessState state;

        public Video(Uri page)
        {
            Address = page;
            State = VideoProcessState.Unprocessed;

            GetId();
        }

        private void GetId()
        {
            string[] parameters = Address.Query.Substring(Address.Query.IndexOf("?") + 1).Split(new string[] { "&" }, StringSplitOptions.RemoveEmptyEntries);
            string idparam = parameters.First(p => p.StartsWith("v="));
            Id = idparam.Substring(idparam.IndexOf("=") + 1);
        }

        public string SanitizeTitle()
        {
            var sb = new StringBuilder();

            for (int i = 0; i < title.Length; i++)
            {
                char filenameChar = title[i];
                foreach (char c in Path.GetInvalidFileNameChars())
                    if (filenameChar.Equals(c))
                    {
                        filenameChar = ' ';
                        break;
                    }

                sb.Append(filenameChar);
            }

            return sb.ToString();
        }

        public string FileName
        {
            get
            {
                return SanitizeTitle() + Quality.Extension;
            }
        }

        public string Title
        {
            get { return title; }
            set { title = value; OnPropertyChanged("Title"); OnPropertyChanged("HasTitle"); }
        }

        public bool HasTitle
        {
            get
            {
                return Title != null && Title.Trim().Length > 0;
            }
        }

        public string Error
        {
            get { return error; }
            set { error = value; OnPropertyChanged("Error"); OnPropertyChanged("HasError"); State = VideoProcessState.Error; }
        }

        public bool HasError
        {
            get
            {
                return Error != null && Error.Trim().Length > 0;
            }
        }

        public QualityLevel Quality
        {
            get { return quality; }
            set { quality = value; OnPropertyChanged("Quality"); }
        }

        public Uri Address
        {
            get { return address; }
            set { address = value; OnPropertyChanged("Address"); }
        }

        public string Id
        {
            get { return id; }
            set { id = value; OnPropertyChanged("Id"); }
        }

        public int L
        {
            get { return l; }
            set { l = value; OnPropertyChanged("L"); }
        }

        public string T
        {
            get { return t; }
            set { t = value; OnPropertyChanged("T"); }
        }

        public VideoProcessState State
        {
            get { return state; }
            set { state = value; OnPropertyChanged("State"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
